package org.jflame.commons.util;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 汉字工具类.
 * 
 * @author charles.zhang
 */
public final class ChineseHelper {

    /**
     * 判断是否是汉字
     * 
     * @param oneChar 字符
     * @return true是汉字
     */
    public static boolean isChinese(char oneChar) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(oneChar);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }

        return false;
    }

    /**
     * 随机产生汉字
     * 
     * @param count 生成的个数
     * @return
     */
    public static String randomChinese(int count) {
        StringBuilder sb = new StringBuilder(count);
        Random random = ThreadLocalRandom.current();
        byte[] b = new byte[2];
        int hightPos;
        int lowPos; // 定义高低位
        for (int i = 0; i < count; i++) {
            hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
            lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
            b[0] = (Integer.valueOf(hightPos)
                    .byteValue());
            b[1] = (Integer.valueOf(lowPos)
                    .byteValue());
            sb.append(new String(b, CharsetHelper.GBK_18030));
        }
        return sb.toString();
    }

}
